package com.gnerv.author.exception;

import com.gnerv.author.entity.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;

/**
 * @description: 全局异常处理
 * @author: Gnerv LiGen
 * @date: 2018-08-01
 **/
@ControllerAdvice
public class GlobalExceptionHandler {

    private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

        /**
     * sql 执行异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = SQLException.class)
    @ResponseBody
    public R sqlErrorHandler(HttpServletRequest req, Exception e) {
        logger.debug(e.getMessage());
        return R.back(R.CODE_500, e.getMessage());
    }

    /**
     * 通用异常处理
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public R defaultErrorHandler(HttpServletRequest req, Exception e) {
        logger.debug(e.getMessage());
        return R.back(R.CODE_500);
    }

}

